#include "scalebarobject.h"

ScaleBarObject::ScaleBarObject() { clear(); }
ScaleBarObject::~ScaleBarObject() { clear(); }

void
ScaleBarObject::clear()
{
  m_pos = QPointF(0,0);
  m_voxels = 10;
  m_type = true; // horizontal
  m_textpos = true; // up/right
}

void
ScaleBarObject::setScaleBar(ScaleBarObject co)
{
  m_pos = co.position();
  m_voxels = co.voxels();
  m_type = co.type();
  m_textpos = co.textpos();
}

void
ScaleBarObject::set(QPointF pos, float vox, bool t, bool tp)
{
  m_pos = pos;
  m_voxels = vox;
  m_type = t;
  m_textpos = tp;
}

void ScaleBarObject::setPosition(QPointF pos) { m_pos = pos; }
void ScaleBarObject::setVoxels(float vox) { m_voxels = vox; }
void ScaleBarObject::setType(bool t) { m_type = t; }
void ScaleBarObject::setTextpos(bool tp) { m_textpos = tp; }

QPointF ScaleBarObject::position() { return m_pos; }
float ScaleBarObject::voxels() { return m_voxels; }
bool ScaleBarObject::type() { return m_type; }
bool ScaleBarObject::textpos() { return m_textpos; }

QList<ScaleBarObject>
ScaleBarObject::interpolate(QList<ScaleBarObject> sb1,
			    QList<ScaleBarObject> sb2,
			    float frc)
{
  QList<ScaleBarObject> sb;
  for (int i=0; i<qMin(sb1.count(),
		       sb2.count()); i++)
    sb.append(interpolate(sb1[i], sb2[i], frc));
  
  int st = sb.count();
  for (int i=st; i<sb1.size(); i++)
    sb.append(sb1[i]);
  
  return sb;
}

ScaleBarObject
ScaleBarObject::interpolate(ScaleBarObject& sb1,
			    ScaleBarObject& sb2,
			    float frc)
{
  ScaleBarObject sb;
  QPointF pos = (1-frc)*sb1.position() + frc*sb2.position();
  sb.setPosition(pos);
  if (frc <=0.5)
    {
      sb.setVoxels(sb1.voxels());
      sb.setType(sb1.type());
      sb.setTextpos(sb1.textpos());
    }
  else
    {
      sb.setVoxels(sb2.voxels());
      sb.setType(sb2.type());
      sb.setTextpos(sb2.textpos());
    }
  return sb;
}

void
ScaleBarObject::load(fstream& fin)
{
  clear();

  int len;
  bool done = false;
  char keyword[100];
  while (!done)
    {
      fin.getline(keyword, 100, 0);

      if (strcmp(keyword, "scalebarobjectend") == 0)
	done = true;
      else if (strcmp(keyword, "position") == 0)
	{
	  float x, y;
	  fin.read((char*)&x, sizeof(float));
	  fin.read((char*)&y, sizeof(float));
	  m_pos = QPointF(x,y);
	}
      else if (strcmp(keyword, "voxels") == 0)
	fin.read((char*)&m_voxels, sizeof(float));
      else if (strcmp(keyword, "type") == 0)
	fin.read((char*)&m_type, sizeof(bool));
      else if (strcmp(keyword, "textpos") == 0)
	fin.read((char*)&m_textpos, sizeof(bool));
    }
}

void
ScaleBarObject::save(fstream& fout)
{
  char keyword[100];
  int len;

  memset(keyword, 0, 100);
  sprintf(keyword, "scalebarobjectstart");
  fout.write((char*)keyword, strlen(keyword)+1);

  float x = m_pos.x();
  float y = m_pos.y();
  memset(keyword, 0, 100);
  sprintf(keyword, "position");
  fout.write((char*)keyword, strlen(keyword)+1);
  fout.write((char*)&x, sizeof(float));
  fout.write((char*)&y, sizeof(float));

  memset(keyword, 0, 100);
  sprintf(keyword, "voxels");
  fout.write((char*)keyword, strlen(keyword)+1);
  fout.write((char*)&m_voxels, sizeof(float));

  memset(keyword, 0, 100);
  sprintf(keyword, "type");
  fout.write((char*)keyword, strlen(keyword)+1);
  fout.write((char*)&m_type, sizeof(bool));

  memset(keyword, 0, 100);
  sprintf(keyword, "textpos");
  fout.write((char*)keyword, strlen(keyword)+1);
  fout.write((char*)&m_textpos, sizeof(bool));

  memset(keyword, 0, 100);
  sprintf(keyword, "scalebarobjectend");
  fout.write((char*)keyword, strlen(keyword)+1);
}

